var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');

var app = getApp();

Page({
  data: {
    couponList: [],
    type: 0,
    typename: '未使用',
    page: 1,
    limit: 10,
    scrollTop: 0
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.getCouponList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    that.getCouponList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getCouponList: function () {
    let that = this;
    that.setData({
      scrollTop: 0,
      couponList: []
    });
    util.request(api.CouponMyList, {
      type: that.data.type,
      page: that.data.page,
      limit: that.data.limit
    }).then(function (res) {
      if (res.result == 1) {
        if (res.data.result.length > 0) {
          that.setData({
            couponList: that.data.couponList.concat(res.data.result),
            page: that.data.page + 1
          });
        } else {
          wx.showToast({
            title: '已经被您看完了!',
            icon: 'none',
            duration: 2000
          });
          return false;
        }
      }
    });
  },

  switchTab: function (e) {
    this.setData({
      couponList: [],
      type: e.currentTarget.dataset.index,
      page: 1,
      limit: 10,
      scrollTop: 0
    });
    if (e.currentTarget.dataset.index == 0) {
      this.setData({
        typename: '未使用'
      });
    }
    if (e.currentTarget.dataset.index == 1) {
      this.setData({
        typename: '已使用'
      });
    }
    if (e.currentTarget.dataset.index == 2) {
      this.setData({
        typename: '已过期'
      });
    }
    this.getCouponList();
  },
})